import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/interest/interest_bean.dart';
import 'package:flutterlib/model/interest/interest_join.dart';

typedef OnAddInterest = void Function(int id);

class InterestListItem extends StatelessWidget {
  final InterestBean interest;

  final VoidCallback onPressed;
  final OnAddInterest onAddInterest;
  final AnimationController animationController;
  final Animation<dynamic> animation;
  final bool addAble;
  final bool isAdd;

  InterestListItem(this.interest,
      {this.onPressed,
      this.animationController,
      this.animation,
      this.addAble : false,
      this.onAddInterest, this.isAdd : false})
      : super();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 10, right: 10, top: 8, bottom: 16),
      child: InkWell(
        splashColor: Colors.transparent,
        onTap: () {
          onPressed();
        },
        child: Container(
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(10.0)),
            boxShadow: <BoxShadow>[
              BoxShadow(
                color: Colors.grey.withOpacity(0.6),
                offset: const Offset(4, 4),
                blurRadius: 16,
              ),
            ],
          ),
          child: ClipRRect(
            borderRadius: const BorderRadius.all(Radius.circular(10.0)),
            child: Stack(
              children: <Widget>[
                Container(
                  color: QuhuiColors.white,
                  height: 134,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(10),
                        child: AspectRatio(
                          aspectRatio: 1.3,
                          child: ExtendedImage.network(
                            interest.groupLogo,
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      Expanded(
                        child: Container(
                          child: Padding(
                            padding: const EdgeInsets.only(
                                left: 0, top: 20, bottom: 20),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Row(
                                  children: [
                                    Expanded(
                                      child: Text("最新话题",
                                          textAlign: TextAlign.left,
                                          style: QuhuiConstant.smallTextBold),
                                    ),
                                  ],
                                ),
                                SizedBox(height: 20),
                                Expanded(
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: hotPosts(interest.recentPost),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      addAble
                          ? getAddBtn(isAdd)
                          : Container(),
                    ],
                  ),
                ),
                Positioned(
                  top: 5,
                  left: 0,
                  child: Container(
                    width: 65,
                    height: 24,
                    decoration: BoxDecoration(
                      color: HexColor('#333333'),
                      borderRadius: const BorderRadius.horizontal(
                          left: Radius.circular(0), right: Radius.circular(5)),
                    ),
                    child: Center(
                        child: Text(
                      interest.name,
                      style: QuhuiConstant.smallTextWhite,
                    )),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  List<Widget> hotPosts(List<Recent_post> recentPost) {
    return recentPost
        .map((e) => Text(e.name,
            textAlign: TextAlign.left, style: QuhuiConstant.minSubText))
        .toList();
  }

  Widget getAddBtn(bool isAdd) {
    return isAdd
        ? Align(
      alignment: Alignment.center,
      child: Container(
        // height: 25.h,
        padding: EdgeInsets.all(5.w),
        margin: EdgeInsets.all(5.w),
        child: Text(
          "已加入",
          style: QuhuiConstant.middleTextRedBold,
        ),
      ),
    )
        : InkWell(
            onTap: () {
              onAddInterest(interest.id);
            },
            child: Align(
              alignment: Alignment.center,
              child: Container(
                // height: 25.h,
                padding: EdgeInsets.all(5.w),
                margin: EdgeInsets.all(5.w),
                decoration: BoxDecoration(
                  color: QuhuiColors.mainThemeColor,
                  borderRadius: BorderRadius.circular(20),
                ),
                child: Text(
                  "加入",
                  style: QuhuiConstant.middleTextWhite,
                ),
              ),
            ),
          );
  }
}
